package thread;

public class ThreadDemo21 {
    private static int x = 0;
    public static void main(String[] args) {
        Object locker = new Object();

        Thread t1 = new Thread(() -> {
            while(true){
                synchronized (locker){
                    System.out.println("t1 获取到 locker");
                    if(x > 10000){
                        break;
                    }else{
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
            System.out.println("t1 任务执行完毕");
        });

        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(100);
                synchronized (locker){
                    System.out.println("t2 线程获取到 locker");
                }
                System.out.println("t2 执行完毕");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                Thread.sleep(100);
                synchronized (locker){
                    System.out.println("t3 线程获取到 locker");
                }
                System.out.println("t3 执行完毕");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread t4 = new Thread(() -> {
            try {
                Thread.sleep(100);
                x = 20000;
                synchronized (locker){
                    System.out.println("t4 获取到 locker");
                    locker.notify();
                }
                System.out.println("t4 执行完毕");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
